package com.gitee.cui.redis.jedis;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import redis.clients.jedis.Jedis;

/**
 * Redis中List类型操作测试
 * @author cuiqingqiang
 * @version 1.0
 * @date 2021/11/7 19:36
 **/
@Slf4j
public class ListTest {

    private static final String KEY = "list1";

    @Test
    public void listTest() {
        // 连接Redis
        Jedis jedis = new Jedis("localhost", 6379);
        log.info("jedis ping: " + jedis.ping());
        jedis.del(KEY);

        // 从List尾部添加3个元素
        jedis.rpush(KEY, "zhangsan", "lisi", "wangwu");

        // 取得类型：list
        log.info("jedis type: " + jedis.type(KEY));
        // 遍历区间[0,-1]，去除全部元素
        log.info("jedis.lrange(KEY, 0, -1): " + jedis.lrange(KEY, 0, -1));
        // 遍历区间[1,2]，取得区间元素
        log.info("jedis.lrange(KEY, 1, 2): " + jedis.lrange(KEY, 1, 2));
        // 取得list的长度
        log.info("jedis.llen(KEY): " + jedis.llen(KEY));
        // 取得下标为1的元素
        log.info("jedis.lindex(KEY, 1): " + jedis.lindex(KEY, 1));
        // 左侧弹出元素
        log.info("jedis.lpop(KEY): " + jedis.lpop(KEY));
        // 右侧弹出元素
        log.info("jedis.rpop(KEY): " + jedis.rpop(KEY));
        //设置下标为0的元素value
        log.info(jedis.lset(KEY, 0, "lisi2"));
        // 遍历区间[0,-1]，取出全部元素
        log.info("jedis.lrange(KEY, 0, -1): " + jedis.lrange(KEY, 0, -1));

        jedis.close();
    }
}
